package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import dao.ParamsDao;

public class QueryAllPrivNameServlet extends HttpServlet {

	public QueryAllPrivNameServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		JSONObject jsonObject = new JSONObject();
		ParamsDao paramsDao = null;
		List<String> list = null;
		
		try {
			paramsDao = new ParamsDao();
			list = paramsDao.getALLPrivName();
			if(list != null && list.size() > 0) {
				JSONArray jsonArray = JSONArray.fromObject(list);
				jsonObject.put("retCode", "1");
				jsonObject.put("privNames", jsonArray);
			} else {
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "无记录");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "数据库异常");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void init() throws ServletException {
	}

}
